/**
 * fileName:com.zimo.web.interceptor.LoginInterceptor.java
 * projectName:Zimo
 */
package com.zimo.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zimo.web.entity.User;

/**
 * 登录验证过滤
 * 
 * @author sg.z/2013-12-16/下午5:05:14
 */
public class Interceptor extends HandlerInterceptorAdapter {

	private Logger log = Logger.getLogger(Interceptor.class);

	/**
	 * 预处理
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		String reqUrl = request.getRequestURI();
		log.debug("# preHandle() 请求的url为：" + reqUrl);
		if (reqUrl.startsWith("/ad/")) {
			User u = (User) request.getSession().getAttribute("user");
			if (u == null) {// 用户登录判断
				return false;
			}else{
				if(!StringUtils.equals("1", u.getIsAdmin())){
					return false;
				}
			}
		}

		return super.preHandle(request, response, handler);
	}

	/**
	 * 每个请求在请求之后，渲染之前的处理
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		User u = (User) request.getSession().getAttribute("user");
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 每个请求渲染之后的处理
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterCompletion(request, response, handler, ex);
	}

}
